package com.honor.SpringBootRepository.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @fileName: MyThreadPoolConfig
 * @author: LmR
 * @createDate: 2024/3/23 22:40
 * @Desc:
 */
@Data
@Configuration
@ConditionalOnProperty(value = "spring.task.my-thread-pool.enabled", havingValue = "true")
@ConfigurationProperties(prefix = "spring.task.my-thread-pool")
public class MyThreadPoolConfig {


    private int corePoolSize;

    private int maxPoolSize;

    private int keepAliveTime;

    private int queueCapacity;


    @Bean("myThreadPool")
    public Executor createMyThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setKeepAliveSeconds(keepAliveTime);
        executor.setQueueCapacity(queueCapacity);
        return executor;
    }

}
